﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SofireXSqlEntityGenerator.Core;

namespace SofireXSqlEntityGenerator.UI
{
    public partial class SqlGeneratorForm : Form
    {
        public SqlGeneratorForm()
        {
            InitializeComponent();

            this.Icon = Properties.Resources.SQL;
            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.CenterParent;
            
            VSEx.FillFieldTypeItems(this.col_FieldType);

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        private readonly SqlGeneratorBase Generator;
        private readonly string TableName;

        private DataTable ColumnsTable;
        public SqlGeneratorForm(SqlGeneratorBase generator, string tableName)
            : this()
        {
            this.Generator = generator;
            this.TableName = tableName;

            if(VSEx.ColumnsCache.TryGetValue(tableName, out this.ColumnsTable))
            {
                dataGridView1.DataSource = this.ColumnsTable;
            }
            else
            {
                this.tsb_Refresh_Click(tsb_Refresh, EventArgs.Empty);
            }
            this.Text = this.Text + "   -   " + this.TableName;
        }

        private string _ResultSql;
        public string ResultSql
        {
            get
            {
                return this._ResultSql;
            }
        }

        private void BuilderSql(SqlOptions options)
        {
            if(dataGridView1.DataSource == null
                || (dataGridView1.IsCurrentCellInEditMode && !dataGridView1.EndEdit())) return;

            List<string> columns = new List<string>();
            List<string> whereColumns = new List<string>();
            foreach(DataRow row in this.ColumnsTable.Rows)
            {
                if(row.Field<bool?>("IsSelected") == true)
                {
                    columns.Add(row.Field<string>("FiledName"));
                }

                if(row.Field<bool?>("IsWhereSelected") == true)
                {
                    whereColumns.Add(row.Field<string>("FiledName"));
                }
            }
            if(columns.Count == 0)
            {
                this.ShowError("请选择需要生成的记录！");
                return;
            }
            options.TableName = this.TableName;
            options.Columns = columns.ToArray();
            options.WhereColumns = whereColumns.ToArray();
            this._ResultSql = this.Generator.CreateSql(options);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void tsb_Refresh_Click(object sender, EventArgs e)
        {
            var r = VSEx.RefreshColumns(this.Generator, this.TableName);
            if(this.IsSuccess(r))
            {
                this.ColumnsTable = r.Value;
                dataGridView1.DataSource = this.ColumnsTable;
            }

            //dataGridView1.AutoResizeColumns();
        }

        private void tsb_Select_Click(object sender, EventArgs e)
        {
            this.BuilderSql(new SqlOptions(SqlOperator.Select));
        }

        private void tsb_Insert_Click(object sender, EventArgs e)
        {
            this.BuilderSql(new SqlOptions(SqlOperator.Insert));
        }

        private void tsb_Update_Click(object sender, EventArgs e)
        {
            this.BuilderSql(new SqlOptions(SqlOperator.Update));
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            this.BuilderSql(new SqlOptions(SqlOperator.Delete));
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if(dataGridView1.IsCurrentCellInEditMode) dataGridView1.EndEdit();
        }

        private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var column = dataGridView1.Columns[e.ColumnIndex];
            if(column.DataPropertyName == "IsSelected" || column.DataPropertyName == "IsWhereSelected")
            {
                if(dataGridView1.IsCurrentCellInEditMode) dataGridView1.EndEdit();
                var isChecked = e.Button == System.Windows.Forms.MouseButtons.Left;
                foreach(DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[e.ColumnIndex].Value = isChecked;
                }
                dataGridView1.Refresh();
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
